Page({
    data: {
      commentList: []
    },
  
    onLoad() {
      this.getAllComments();
    },
  
    formatDate(dateStr) {
      const date = new Date(dateStr);
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    },
  
    getAllComments() {
      wx.request({
        url: 'http://localhost:8080/comment/list',
        method: 'POST',
        data: { page: 1, limit: 100 },
        success: (res) => {
          if (res.data.code === 0) {
              console.log(res.data.data)
            const list = res.data.data.map(item => ({
              ...item,
              reserveTimeFormat: this.formatDate(item.reserveTime),
              expand: false,
              showToggle: item.comment.length >= 50
            }));
            this.setData({ commentList: list });
          } else {
            wx.showToast({ title: res.data.msg || '获取失败', icon: 'none' });
          }
        }
      });
    },
  
    toggleExpand(e) {
      const index = e.currentTarget.dataset.index;
      const list = this.data.commentList;
      list[index].expand = !list[index].expand;
      this.setData({ commentList: list });
    },
  
    replyToComment(e) {
      const index = e.currentTarget.dataset.index;
      const item = this.data.commentList[index];
      wx.showModal({
        title: '回复评论',
        editable: true,
        placeholderText: '请输入回复内容',
        success: (res) => {
          if (res.confirm && res.content) {
            this.submitReply(item.commentId, res.content, wx.getStorageSync('userInfo').userId); // 管理员ID默认写死为999，可改为实际值
          }
        }
      });
    },
  
    submitReply(id, replay, replayAdminId) {
      wx.request({
        url: 'http://localhost:8080/comment/reply?id='+ id + '&replay=' + replay + '&replayAdminId=' + replayAdminId,
        method: 'POST',
        // data: {
        //   Id: id,
        //   replay,
        //   replayAdminId
        // },
        // success: (res) => {
        //   if (res.data.code === 200) {
        //     wx.showToast({ title: '回复成功', icon: 'success' });
        //     this.getAllComments();
        //     this.onLoad();
        //   } else {
        //     wx.showToast({ title: res.data.msg || '回复失败', icon: 'none' });
        //   }
        // }
        success: (res) => {
            if (res.data.code === 200) {
              wx.showToast({ title: '回复成功', icon: 'success' });
      
              // ✅ 更新本地 commentList
              const list = this.data.commentList;
              const index = list.findIndex(item => item.reserveId === id);
              if (index !== -1) {
                list[index].replay = replay;
                list[index].replayTime = this.formatDate(new Date()); // 回复时间可以用当前时间
                list[index].showToggle1 = replay.length >= 50;
              }
              this.setData({ commentList: list });
            } else {
              wx.showToast({ title: res.data.msg || '回复失败', icon: 'none' });
            }
          }
      });
    }
  });
  